import { IQueryObject } from 'tiny-entity2/queryObject';
import { ServiceException } from './../common/excpetion/serviceException';
import { Transaction } from "tiny-entity2";
import { ApiDataContext } from "../domain/apiDataContext";
import { DataSource } from "../domain/models/dataSource";
import { BaseService } from "./baseService";

export class DataSourceService extends BaseService {
    DeleteDataSourceById(id: string, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            await ctx.Delete<DataSource>(x => x.id == $args1, ctx.DataSource, { $args1: id });
        });
    }
    DeleteDataSourceByObjectId(objectId: string, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            await ctx.Delete<DataSource>(x => x.objectId == $args1, ctx.DataSource, { $args1: objectId });
        });
    }
    GetDataSourceByObjectId(id: string) {
        let ctx = new ApiDataContext();
        return ctx.DataSource.First(x => x.objectId == $args1, { $args1: id });
    }

    CreateDataSource(entity: DataSource, ctx?: ApiDataContext, checkName = true) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let has = await ctx.DataSource.First(x => x.name == $args1 && x.orgId == $args2, { $args1: entity.name, $args2: entity.orgId  });
            if (has && checkName) {
                throw new ServiceException(`已经存在名称为【${entity.name}】的数据源！`);
            }

            let d = new DataSource();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    UpdateDataSource(entity: DataSource, ctx?: ApiDataContext, checkName = false) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let has = await ctx.DataSource.First(x => x.name == $args1 && x.id != $args2 && x.orgId == $args3, { $args1: entity.name, $args2: entity.id, $args3: entity.orgId });
            if (has) {
                throw new ServiceException(`已经存在名称为【${entity.name}】的数据源！`);
            }
            let d = new DataSource();
            d.ConverToEntity(entity);
            return ctx.Update(d);
        });
    }
    GetDataSourceById(id: string) {
        let ctx = new ApiDataContext();
        return ctx.DataSource.First(x => x.id == $args1, { $args1: id });
    }
    QueryDataSource(params: DataSourceQueryParams) {
        let ctx = new ApiDataContext();
        let q = ctx.DataSource as IQueryObject<DataSource>;
        if (params) {
            if (params.name) {
                q = q.Where(x => x.name == $args1, { $args1: params.name });
            }
            if (params.dataType) {
                q = q.Where(x => x.dataType == $args1, { $args1: params.dataType });
            }
            if (params.objectId) {
                q = q.Where(x => x.objectId == $args1, { $args1: params.objectId });
            }
            if (params.orgId) {
                q = q.Where(x => x.orgId == $args1 || x.orgId == 'global', { $args1: params.orgId });
            }
        }

        return q.OrderBy(x => x.name).ToList();
    }
}

export interface DataSourceQueryParams {
    name?: string;
    dataType?: string;
    objectId?: string;
    orgId?: string;
}